﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;
using org.in2bits.MyXls;

namespace CommunitySystem
{
    public partial class MianShiControl_ZLGL : UserControl
    {
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();
        private ObservableCollection<ZhaoLuJiHuaModel> poi =
            new ObservableCollection<ZhaoLuJiHuaModel>();
        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> selectedempmodel =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        public string DaoChuZu { get; set; }
        public string NiTF { get; set; }
        public ObservableCollection<ZhaoLuZhiWeiModel> tempmodels = new ObservableCollection<ZhaoLuZhiWeiModel>();
        public MianShiControl_ZLGL()
        {
            InitializeComponent();
            //for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            //{
            //    ComboBoxItem cbi = new ComboBoxItem();
            //    cbi.Content = i.ToString();
            //    cbi.Tag = i;
            //    nian.Items.Add(cbi);
            //}
            //nian.SelectedIndex = 0;
            Client1.GetZhaoLu_DaoRuKaoShengModelCompleted += new EventHandler<GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs>(Client1_GetZhaoLu_DaoRuKaoShengModelCompleted);
         //   Client1.NiTongYiorNot_ZLCompleted += new EventHandler<NiTongYiorNot_ZLCompletedEventArgs>(Client1_NiTongYiorNot_ZLCompleted);

            Client1.ISNiTongYiCompleted += new EventHandler<ISNiTongYiCompletedEventArgs>(Client1_ISNiTongYiCompleted);
            
            Client1.QiQuanCompleted += new EventHandler<QiQuanCompletedEventArgs>(Client1_QiQuanCompleted);
            Client1.UpdateZL_MianShiFenShuCompleted += new EventHandler<UpdateZL_MianShiFenShuCompletedEventArgs>(Client1_UpdateZL_MianShiFenShuCompleted);
            Client1.GetZhaoLuJiHuaModelForComCompleted += new EventHandler<GetZhaoLuJiHuaModelForComCompletedEventArgs>(Client1_GetZhaoLuJiHuaModelForComCompleted);
            Client1.GetZhaoLuJiHuaModelForComAsync();
            Client1.GetZhaoLuZhiWeiForMianShiCompleted += new EventHandler<GetZhaoLuZhiWeiForMianShiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiForMianShiCompleted);
            
            //Client1.UpdateZL_MianShiFenShu_LuRuWanBiCompleted += new EventHandler<UpdateZL_MianShiFenShu_LuRuWanBiCompletedEventArgs>(Client1_UpdateZL_MianShiFenShu_LuRuWanBiCompleted);
            Client1.UpdateZL_MianShiFenShu_LuRuWanBiBySqlCompleted += new EventHandler<UpdateZL_MianShiFenShu_LuRuWanBiBySqlCompletedEventArgs>(Client1_UpdateZL_MianShiFenShu_LuRuWanBiBySqlCompleted);
          
            zucha.DataContext = this;
            nitongyifou.DataContext = this;
        }

       

       

        

        void Client1_GetZhaoLuZhiWeiForMianShiCompleted(object sender, GetZhaoLuZhiWeiForMianShiCompletedEventArgs e)
        {
            tempmodels = e.Result;
        }

        void Client1_GetZhaoLuJiHuaModelForComCompleted(object sender, GetZhaoLuJiHuaModelForComCompletedEventArgs e)
        {
            poi = e.Result;
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            poi1.Add(poi.Where(p => p.IsLocked == false).FirstOrDefault());
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }
        private int tiaojian = 0;
        void gangwei_SelectionChanged1(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                tiaojian = ((ZhaoLuJiHuaModel)(((ComboBox)sender).SelectedItem)).ID;
            }
        }

       /// <summary>
        /// 输入分数
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (isSR)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text);
                    if (Convert.ToDecimal(((TextBox)sender).Text) > 100)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("输入的成绩有误，请重新输入！");
                        return;
                    }
                    //输入分数 及时保存
                    var obj = (sender as TextBox).Tag as ZhaoLu_DaoRuKaoShengModel;
                    obj.MianShiScore = Convert.ToDecimal((sender as TextBox).Text);
                    Client1.UpdateZL_MianShiFenShuAsync(obj);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    ((TextBox)sender).Text = "";
                }
            }
           
        }
        bool isSR = false;
        void tv_GotFocus(object sender, RoutedEventArgs e)
        {
            isSR = true;
        }

        private void Text_LostFocus(object sender, EventArgs e)
        {
            isSR = false;
            //string YRBM =
            //    temp.Where(p => p.ID == Convert.ToInt32(((TextBox) sender).Tag)).FirstOrDefault().YRBM;
            //string BBZW =
            //    temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().BBZW;
            //List<ZhaoLu_DaoRuKaoShengModel> models = temp.Where(p => p.YRBM == YRBM && p.BBZW == BBZW).ToList();
            //List<string> ss = new List<string>();
            //foreach (ZhaoLu_DaoRuKaoShengModel i in models)
            //{
            //    ss.Add(i.Sec.ToString());
            //}
            
            //if (!string.IsNullOrEmpty(((TextBox)sender).Text))
            //{
            //    if (Convert.ToDecimal(((TextBox)sender).Text) > 100)
            //    {
            //        SilverlightMessageBoxLibrary.Message.ErrorMessage("输入的成绩有误，请重新输入！");
            //        return;
            //    }
            //    if (temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault() != null)
            //    {
            //        temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().MianShiScore = Convert.ToDecimal(((TextBox)sender).Text);
            //        temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().MianShiZheSuan = Convert.ToDecimal(((TextBox)sender).Text)/100*60;
            //        temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().TotalScore =
            //            Convert.ToDecimal(Math.Round(
            //                    (Math.Round(Convert.ToDouble(temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().ZongFen) * 40/250, 2) +
            //                     Math.Round(Convert.ToDouble(((TextBox)sender).Text) * 0.6, 2)), 2).ToString());
            //    }
              
            //    for (int i = 0; i < models.OrderByDescending(p => p.TotalScore).Count(); i++)
            //    {
            //        models.OrderByDescending(p => p.TotalScore).ToList()[i].Sec = Convert.ToInt32(ss[i]);
            //        models.OrderByDescending(p => p.TotalScore).ToList()[i].MingCi = i + 1;
            //    }
            //}
        }
        DateTime? dt = null;
        public void Load()
        {
            Client1.GetZhaoLu_DaoRuKaoShengModelAsync(tiaojian, false, 0, 0, NiTF, "", "", !string.IsNullOrEmpty(riqi11.Text) ? Convert.ToDateTime(riqi11.Text) : dt,
                DaoChuZu, ((ComboBoxItem)status.SelectedItem).Content.ToString(), "0");
            Client1.GetZhaoLuZhiWeiForMianShiAsync(tiaojian);
        }

        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> temp =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        void Client1_GetZhaoLu_DaoRuKaoShengModelCompleted(object sender, GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs e)
        {
             temp=  (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)e.Result;
            //List<string> tempstring = new List<string>();
            //if (temp != null && temp.Count > 0)
            //{
            //    for (int i = 0; i < temp.Count; i++)
            //    {
            //        if (!tempstring.Contains(temp[i].YRBM + "-" + temp[i].BBZW ))
            //            tempstring.Add(temp[i].YRBM + "-" + temp[i].BBZW);
            //        temp[i].Sec = i + 1;

            //    }
            //    for (int i = 0; i < tempstring.Count; i++)
            //    {
            //        for (int n = 0; n < temp.Where(p => p.YRBM == tempstring[i].Split('-')[0] &&
            //                                                            p.BBZW == tempstring[i].Split('-')[1]).Count(); n++)
            //        {
            //            if (temp.Where(p => p.YRBM == tempstring[i].Split('-')[0] &&
            //                            p.BBZW == tempstring[i].Split('-')[1]).ToList()[n].MianShiScore != 0)
            //            temp.Where(p => p.YRBM == tempstring[i].Split('-')[0] &&
            //                            p.BBZW == tempstring[i].Split('-')[1] ).ToList()[n].MingCi = n + 1;
            //        }
            //    }
            //}

             for (int i = 0; i < temp.Count; i++)
             {
                 temp[i].Sec = i + 1;
             }
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = temp;
        }
        /// <summary>
        /// 保存面试成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Data_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            count = 0;
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel model in selectedempmodel)
                {
                    Client1.UpdateZL_MianShiFenShuAsync(model);
                    count = count + 1;
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要保存的面试结果！"); 
        }

        private int count = 0;
        void Client1_UpdateZL_MianShiFenShuCompleted(object sender, UpdateZL_MianShiFenShuCompletedEventArgs e)
        {
            //if (count == selectedempmodel.Count)
            //{
            //    count = count - 1;
            //    SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功!");
            Load();
            //}
            
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }
        /// <summary>
        /// 拰同意
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nitongyi_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0) //&& selectedempmodel.Count == 1
            {
                if(selectedempmodel.Where(p=>p.MianShiScore <=0).FirstOrDefault() != null)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员还有未输入面试成绩，不可拟同意！");
                    return;
                }
                if (selectedempmodel.Where(p => p.IsFangQi == true || p.IsTongYi == "拟同意").FirstOrDefault() != null)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员状态不可拟同意！");
                    return;
                }
                if (tempmodels.Where(p => p.ZhiWeiDaiMa == selectedempmodel[0].ZWID).FirstOrDefault().PeopleCount <=
                    ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).
                    Where(m => m.ZWID == selectedempmodel[0].ZWID).Where(m=>m.IsTongYi == "拟同意").Count())
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("本岗位已招满人员，不可拟同意！");
                    return;
                }
                if (MessageBox.Show("确定要拟同意所选人员吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                   // ObservableCollection<int> ids = new ObservableCollection<int>();
                    string ids = "";
                    foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                    {
                       // ids.Add(i.ID);
                        ids += i.ID.ToString() + ",";
                    }
                   // Client1.NiTongYiorNot_ZLAsync(ids, "拟同意");
                    Client1.ISNiTongYiAsync(ids.TrimEnd(','), "拟同意");
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要拟同意的人员！");
        }
        /// <summary>
        /// 取消拟同意
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nitongyi_Click1(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (selectedempmodel.Where(p => p.MianShiScore <= 0).FirstOrDefault() != null)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员还有未输入面试成绩，不可取消拟同意！");
                    return;
                }
                if (selectedempmodel.Where(p => p.IsFangQi == true || string.IsNullOrEmpty(p.IsTongYi)).FirstOrDefault() != null)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员状态不可取消拟同意！");
                    return;
                }
                if (MessageBox.Show("确定要取消拟同意所选人员吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                  //  ObservableCollection<int> ids = new ObservableCollection<int>();
                    string ids = "";
                    foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                    {
                       // ids.Add(i.ID);
                        ids += i.ID.ToString() + ",";
                    }
                   // Client1.NiTongYiorNot_ZLAsync(ids, "");
                    Client1.ISNiTongYiAsync(ids.TrimEnd(','), "拟同意");

                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要取消拟同意的人员！");
        }

        void Client1_NiTongYiorNot_ZLCompleted(object sender, NiTongYiorNot_ZLCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("成功!");
                Load();
            }
        }
        void Client1_ISNiTongYiCompleted(object sender, ISNiTongYiCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("成功!");
                Load();
            }
        }
        /// <summary>
        /// 弃权
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fangqi_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定进行弃权操作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ss = "";
                    foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                    {
                        if (i.IsTongYi == "拟同意")
                        {
                            ss += i.ID.ToString() + ",";
                        }
                        else
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员没有拟同意，不可弃权！");
                            ss = "";
                            return;
                        }
                    }
                
                    Client1.QiQuanAsync(ss.Substring(0, ss.Length - 1), "体检前");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要弃权的人员！"); 
        }
        void Client1_QiQuanCompleted(object sender, QiQuanCompletedEventArgs e)
        {
            MessageBox.Show("弃权成功！");
            Load();
        }
        private Stream ss = null;
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                var downLoad = new SaveFileDialog()
                {
                    //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls", //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                    Filter = "Excel files(2003) (*.xls)|*.xls",
                };
                if (downLoad.ShowDialog() == true)
                {
                    ss = downLoad.OpenFile();
                    PageInit();
                }
            }
        }
        public void PageInit()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.TextWrapRight = true;

            XF xf1 = xls.NewXF();
            xf1.UseBorder = true;
            xf1.TopLineStyle = 1;
            xf1.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.BottomLineStyle = 1;
            xf1.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.LeftLineStyle = 1;
            xf1.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.RightLineStyle = 1;
            xf1.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 15 * 20;

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;
            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            xf3.Font.Bold = true;
            xf3.Font.Height = 12 * 20;
            string yueri = "";
            if(!string.IsNullOrEmpty(riqi11.Text) )
            {
                yueri = Convert.ToDateTime(riqi11.Text).Month + "月" + Convert.ToDateTime(riqi11.Text).Day + "日";
            }
            string shangxiawu = "";
            if((ComboBoxItem)status.SelectedItem != null)
            {
                shangxiawu = ((ComboBoxItem) status.SelectedItem).Content.ToString();
            }
            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("人员面试表");
            sheet1.Cells.Merge(1, 1, 1, 14);
            sheet1.Cells.Add(1, 1, "徐汇区" + Convert.ToDateTime(selectedempmodel[0].AddDay).Year + "年公务员招考面试人员成绩汇总表（" + yueri + shangxiawu + "  " + DaoChuZu + "）", xf1);
            sheet1.Cells.Add(2, 1, "序号", xf3);
            sheet1.Cells.Add(2, 2, "组别", xf3);
            sheet1.Cells.Add(2, 3, "面试日期", xf3);
            sheet1.Cells.Add(2, 4, "用人单位", xf3);
            sheet1.Cells.Add(2, 5, "报考职务", xf3);
            sheet1.Cells.Add(2, 6, "抽签序号", xf3);
            sheet1.Cells.Add(2, 7, "姓名", xf3);
            sheet1.Cells.Add(2, 8, "笔试分数", xf3);
            sheet1.Cells.Add(2, 9, "分值折算40%", xf3);
            sheet1.Cells.Add(2, 10, "面试分数", xf3);
            sheet1.Cells.Add(2, 11, "分值折算60%", xf3);
            sheet1.Cells.Add(2, 12, "考生总分", xf3);
            sheet1.Cells.Add(2, 13, "名次", xf3);
            sheet1.Cells.Add(2, 14, "备注", xf3);

            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 18 * 256;
            colInfoxueli1.ColumnIndexStart = 2;
            colInfoxueli1.ColumnIndexEnd = 2;
            sheet1.AddColumnInfo(colInfoxueli1);

            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            colInfoxueli2.Width = 25 * 256;
            colInfoxueli2.ColumnIndexStart = 3;
            colInfoxueli2.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli2);

            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 28 * 256;
            colInfoxueli3.ColumnIndexStart = 4;
            colInfoxueli3.ColumnIndexEnd = 4;
            sheet1.AddColumnInfo(colInfoxueli3);

            ColumnInfo colInfoxueli7 = new ColumnInfo(xls, sheet1);
            colInfoxueli7.Width = 20 * 256;
            colInfoxueli7.ColumnIndexStart = 5;
            colInfoxueli7.ColumnIndexEnd = 5;
            sheet1.AddColumnInfo(colInfoxueli7);

            ColumnInfo colInfoxueli8 = new ColumnInfo(xls, sheet1);
            colInfoxueli8.Width = 15 * 256;
            colInfoxueli8.ColumnIndexStart = 10;
            colInfoxueli8.ColumnIndexEnd = 10;
            sheet1.AddColumnInfo(colInfoxueli8);

            ColumnInfo colInfoxueli9 = new ColumnInfo(xls, sheet1);
            colInfoxueli9.Width = 15 * 256;
            colInfoxueli9.ColumnIndexStart = 8;
            colInfoxueli9.ColumnIndexEnd = 8;
            sheet1.AddColumnInfo(colInfoxueli9);

            ColumnInfo colInfoxueli10 = new ColumnInfo(xls, sheet1);
            colInfoxueli10.Width = 18 * 256;
            colInfoxueli10.ColumnIndexStart = 9;
            colInfoxueli10.ColumnIndexEnd = 9;
            sheet1.AddColumnInfo(colInfoxueli10);

            ColumnInfo colInfoxueli11 = new ColumnInfo(xls, sheet1);
            colInfoxueli11.Width = 18 * 256;
            colInfoxueli11.ColumnIndexStart = 11;
            colInfoxueli11.ColumnIndexEnd = 11;
            sheet1.AddColumnInfo(colInfoxueli11);

            for (int i = 0; i < selectedempmodel.Count; i++)
            {
                sheet1.Cells.Add(i + 3, 1, i + 1, xf);
                if (selectedempmodel[i].Zu != null) sheet1.Cells.Add(i + 3, 2, selectedempmodel[i].Zu.ToString(), xf);
                if (selectedempmodel[i].RiQi != null) sheet1.Cells.Add(i + 3, 3, Convert.ToDateTime(selectedempmodel[i].RiQi).ToString("yyyy-MM-dd"), xf);
                if (selectedempmodel[i].YRBM != null) sheet1.Cells.Add(i + 3, 4, selectedempmodel[i].YRBM.ToString(), xf);
                if (selectedempmodel[i].BBZW != null) sheet1.Cells.Add(i + 3,5, selectedempmodel[i].BBZW.ToString(), xf);
                sheet1.Cells.Add(i + 3, 6, "", xf);
                if (selectedempmodel[i].XingMing != null) sheet1.Cells.Add(i + 3, 7, selectedempmodel[i].XingMing.ToString(), xf);
                if (selectedempmodel[i].ZongFen != null) sheet1.Cells.Add(i + 3, 8, selectedempmodel[i].ZongFen.ToString(), xf);
                if (selectedempmodel[i].BiShiZheSuan != null) sheet1.Cells.Add(i + 3, 9, selectedempmodel[i].BiShiZheSuan.ToString(), xf);
                if (selectedempmodel[i].MianShiScore != null) sheet1.Cells.Add(i + 3, 10, selectedempmodel[i].MianShiScore.ToString(), xf);
                if (selectedempmodel[i].MianShiZheSuan != null) sheet1.Cells.Add(i + 3, 11, selectedempmodel[i].MianShiZheSuan.ToString(), xf);
                if (selectedempmodel[i].TotalScore != null) sheet1.Cells.Add(i + 3, 12, selectedempmodel[i].TotalScore.ToString(), xf);
                sheet1.Cells.Add(i + 3, 13, selectedempmodel[i].MingCi.ToString(), xf);
                sheet1.Cells.Add(i + 3, 14, "", xf);
            }
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (ZhaoLu_DaoRuKaoShengModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }
        }
        
        /// <summary>
        /// 录入完毕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LRWB_Click(object sender, RoutedEventArgs e)
        {
            foreach(ZhaoLuZhiWeiModel i in tempmodels)
            {
                int cou = i.PeopleCount;
                int oldcou = i.PeopleCount;
                foreach(ZhaoLu_DaoRuKaoShengModel p in  ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Where(m=>m.ZWID == i.ZhiWeiDaiMa))
                {
                    if (cou == 0)
                    {
                        p.MianShiBeiZhu = "";
                    }
                    if(cou != 0)
                    {
                        if(p.MianShiScore >=60 )
                        {
                            if(p.IsFangQi == false)
                            {
                                p.IsTongYi = "拟同意";
                                p.MianShiBeiZhu = "拟同意";
                                
                            }
                            cou = cou - 1;
                            
                        }
                        
                    }
                    
                }
                if(((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Where(m=>m.ZWID == i.ZhiWeiDaiMa).Where(m=>m.IsTongYi == "拟同意").Count() == 0)
                {
                    foreach (ZhaoLu_DaoRuKaoShengModel p in ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Where(m => m.ZWID == i.ZhiWeiDaiMa))
                    {
                        p.MianShiBeiZhu = "岗位未招录";
                    }
                }
                if (((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Where(m => m.ZWID == i.ZhiWeiDaiMa).Where(m => m.IsTongYi == "拟同意").Count() > 0
                    && ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Where(m => m.ZWID == i.ZhiWeiDaiMa).Where(m => m.IsTongYi == "拟同意").Count() < oldcou)
                {
                    foreach (ZhaoLu_DaoRuKaoShengModel p in ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Where(m => m.ZWID == i.ZhiWeiDaiMa))
                    {
                        if(string.IsNullOrEmpty(p.IsTongYi))
                            p.MianShiBeiZhu = "岗位未招满";
                    }
                }
            }
             string sql = "";
            foreach (ZhaoLu_DaoRuKaoShengModel model in ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource))
            {

                Client1.UpdateZL_MianShiFenShu_LuRuWanBiAsync(model);
                count1 = count1 + 1;
                //zl.Add(model);
               //  sql +=string.Format(" update ZhaoLu_DaoRuKaoSheng set IsTongYi = '{0}',MianShiBeiZhu = '{1}' where ID ={2} ;",model.IsTongYi,model.MianShiBeiZhu,model.ID);
            }
          //  Client1.UpdateZL_MianShiFenShu_LuRuWanBiBySqlAsync(sql);
        }
        void Client1_UpdateZL_MianShiFenShu_LuRuWanBiBySqlCompleted(object sender, UpdateZL_MianShiFenShu_LuRuWanBiBySqlCompletedEventArgs e)
        {
            if(e.Result)
            SilverlightMessageBoxLibrary.Message.InfoMessage("录入成功!");

            Load();
        }

        private int count1 = 0;
        void Client1_UpdateZL_MianShiFenShu_LuRuWanBiCompleted(object sender, UpdateZL_MianShiFenShu_LuRuWanBiCompletedEventArgs e)
        {
            if (count1 == selectedempmodel.Count)
            {
                count1 = count1 - 1;
                SilverlightMessageBoxLibrary.Message.InfoMessage("录入成功!");
               
                Load();
            }
            return;
        }

         ObservableCollection<ZhaoLuZhiWeiModel> tempZhaoLuZhiWeiModel =
                new ObservableCollection<ZhaoLuZhiWeiModel>();
        /// <summary>
         /// 导出空缺职位表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Export_Click(object sender, RoutedEventArgs e)
        {
            
            foreach (ZhaoLuZhiWeiModel i in tempmodels)
            {
                if (((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Where(m => m.ZWID == i.ZhiWeiDaiMa).Where(m => m.IsTongYi == "拟同意").Count() <i.PeopleCount)
                {
                    i.PeopleCount = i.PeopleCount -
                                    ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>) DGRole.ItemsSource).Where(
                                        m => m.ZWID == i.ZhiWeiDaiMa).Where(m => m.IsTongYi == "拟同意").Count();
                    tempZhaoLuZhiWeiModel.Add(i);
                }
            }
            if (tempZhaoLuZhiWeiModel.Count > 0)
            {
                var downLoad = new SaveFileDialog()
                {
                    //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls", //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                    Filter = "Excel files(2003) (*.xls)|*.xls",
                };
                if (downLoad.ShowDialog() == true)
                {
                    ss = downLoad.OpenFile();
                    PageInit1();
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("没有空缺职位！"); 
        }
        public void PageInit1()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.TextWrapRight = true;

            XF xf1 = xls.NewXF();
            xf1.UseBorder = true;
            xf1.TopLineStyle = 1;
            xf1.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.BottomLineStyle = 1;
            xf1.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.LeftLineStyle = 1;
            xf1.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.RightLineStyle = 1;
            xf1.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 15 * 20;

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;
            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            xf3.Font.Bold = true;
            xf3.Font.Height = 12 * 20;
            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("徐汇区公务员招考空缺职位汇总表");
            sheet1.Cells.Merge(1, 1, 1, 14);
            sheet1.Cells.Add(1, 1, "徐汇区公务员招考空缺职位汇总表", xf1);
            sheet1.Cells.Add(2, 1, "序号", xf3);
            sheet1.Cells.Add(2, 2, "招录机关", xf3);
            sheet1.Cells.Add(2, 3, "用人部门", xf3);
            sheet1.Cells.Add(2, 4, "职位名称", xf3);
            sheet1.Cells.Add(2, 5, "职位代码", xf3);
            sheet1.Cells.Add(2, 6, "职位简介", xf3);
            sheet1.Cells.Add(2, 7, "招录人数", xf3);
            sheet1.Cells.Add(2, 8, "基层工作最低年限", xf3);
            sheet1.Cells.Add(2, 9, "招考对象", xf3);
            sheet1.Cells.Add(2, 10, "专业（本科）", xf3);
            sheet1.Cells.Add(2, 11, "专业（研究生）", xf3);
            sheet1.Cells.Add(2, 12, "学历要求", xf3);
            sheet1.Cells.Add(2, 13, "学位要求", xf3);
            sheet1.Cells.Add(2, 14, "政治面貌", xf3);

            sheet1.Cells.Add(2, 15, "专业考试科目", xf3);
            sheet1.Cells.Add(2, 16, "其他条件", xf3);
            sheet1.Cells.Add(2, 17, "备注", xf3);
            sheet1.Cells.Add(2, 18, "审核备注", xf3);

            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            //colInfoxueli2.Hidden = true;
            colInfoxueli2.Width = 28 * 256;
            colInfoxueli2.ColumnIndexStart = 2;
            colInfoxueli2.ColumnIndexEnd = 2;
            sheet1.AddColumnInfo(colInfoxueli2);

            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 23 * 256;
            colInfoxueli3.ColumnIndexStart = 3;
            colInfoxueli3.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli3);

            ColumnInfo colInfoxueli7 = new ColumnInfo(xls, sheet1);
            colInfoxueli7.Width = 35 * 256;
            colInfoxueli7.ColumnIndexStart = 5;
            colInfoxueli7.ColumnIndexEnd = 5;
            sheet1.AddColumnInfo(colInfoxueli7);

            ColumnInfo colInfoxueli8 = new ColumnInfo(xls, sheet1);
            colInfoxueli8.Width = 15 * 256;
            colInfoxueli8.ColumnIndexStart = 8;
            colInfoxueli8.ColumnIndexEnd = 8;
            sheet1.AddColumnInfo(colInfoxueli8);

            ColumnInfo colInfoxueli9 = new ColumnInfo(xls, sheet1);
            colInfoxueli9.Width = 20 * 256;
            colInfoxueli9.ColumnIndexStart = 9;
            colInfoxueli9.ColumnIndexEnd = 9;
            sheet1.AddColumnInfo(colInfoxueli9);

            ColumnInfo colInfoxueli10 = new ColumnInfo(xls, sheet1);
            colInfoxueli10.Width = 20 * 256;
            colInfoxueli10.ColumnIndexStart = 10;
            colInfoxueli10.ColumnIndexEnd = 10;
            sheet1.AddColumnInfo(colInfoxueli10);

            ColumnInfo colInfoxueli11 = new ColumnInfo(xls, sheet1);
            colInfoxueli11.Width = 25 * 256;
            colInfoxueli11.ColumnIndexStart = 15;
            colInfoxueli11.ColumnIndexEnd = 15;
            sheet1.AddColumnInfo(colInfoxueli11);

            ColumnInfo colInfoxueli17 = new ColumnInfo(xls, sheet1);
            colInfoxueli17.Width = 20 * 256;
            colInfoxueli17.ColumnIndexStart = 16;
            colInfoxueli17.ColumnIndexEnd = 16;
            sheet1.AddColumnInfo(colInfoxueli17);

            for (int i = 0; i < tempZhaoLuZhiWeiModel.Count; i++)
            {
                sheet1.Cells.Add(i + 3, 1, i + 1, xf);
                if (tempZhaoLuZhiWeiModel[i].Department != null) sheet1.Cells.Add(i + 3, 2, tempZhaoLuZhiWeiModel[i].Department.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].OrgName != null) sheet1.Cells.Add(i + 3, 3, tempZhaoLuZhiWeiModel[i].OrgName, xf);
                if (tempZhaoLuZhiWeiModel[i].Duty != null) sheet1.Cells.Add(i + 3, 4, tempZhaoLuZhiWeiModel[i].Duty.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].ZhiWeiDaiMa != null) sheet1.Cells.Add(i + 3, 5, tempZhaoLuZhiWeiModel[i].ZhiWeiDaiMa.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].DutyDescription != null) sheet1.Cells.Add(i + 3, 6, tempZhaoLuZhiWeiModel[i].DutyDescription.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].PeopleCount != null) sheet1.Cells.Add(i + 3, 7, tempZhaoLuZhiWeiModel[i].PeopleCount.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].JCGZZDNX != null) sheet1.Cells.Add(i + 3, 8, tempZhaoLuZhiWeiModel[i].JCGZZDNX.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].ZKDX != null) sheet1.Cells.Add(i + 3, 9, tempZhaoLuZhiWeiModel[i].ZKDX.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].BenKeZhuanYe != null) sheet1.Cells.Add(i + 3, 10, tempZhaoLuZhiWeiModel[i].BenKeZhuanYe.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].YanJiuShengZhuanYe != null) sheet1.Cells.Add(i + 3, 11, tempZhaoLuZhiWeiModel[i].YanJiuShengZhuanYe.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].XueLiYaoQiu != null) sheet1.Cells.Add(i + 3, 12, tempZhaoLuZhiWeiModel[i].XueLiYaoQiu.ToString(), xf);

                if (tempZhaoLuZhiWeiModel[i].XueWeiYaoQiu != null) sheet1.Cells.Add(i + 3, 13, tempZhaoLuZhiWeiModel[i].XueWeiYaoQiu.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].Political != null) sheet1.Cells.Add(i + 3, 14, tempZhaoLuZhiWeiModel[i].Political.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].ZYKSKM != null) sheet1.Cells.Add(i + 3, 15, tempZhaoLuZhiWeiModel[i].ZYKSKM.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].Other != null) sheet1.Cells.Add(i + 3, 16, tempZhaoLuZhiWeiModel[i].Other.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].Memo != null) sheet1.Cells.Add(i + 3, 17, tempZhaoLuZhiWeiModel[i].Memo.ToString(), xf);
                if (tempZhaoLuZhiWeiModel[i].CheckMemo != null) sheet1.Cells.Add(i + 3, 18, tempZhaoLuZhiWeiModel[i].CheckMemo.ToString(), xf);
            }
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
        }

        private string islock = "0";
        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            islock = "0";
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            poi1.Add(poi.Where(p => p.IsLocked == false).FirstOrDefault());
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            islock = "1";
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            foreach (ZhaoLuJiHuaModel i in poi)
            {
                if (i.IsLocked) poi1.Add(i);
            }
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }
    }
}
